import { queryTakeUserList } from '../service';


const exportTypeOptions = [
  { label: '订单导出', value: 1 },
  { label: '报表导出', value: 8 },
]

const stateOptions = [
  { label: '待执行', value: 0 },
  { label: '执行中', value: 1 },
  { label: '已完成', value: 2 },
  { label: '执行失败', value: 3 },
]

//表单配置项
const formItemData = {
  'exportType': {
    itemConfig: {
      name: 'exportType'
    },
    elConfig: {
      formType: 'Select',
      placeholder: '任务类型',
      options: exportTypeOptions
    }
  },
  'state': {
    itemConfig: {
      name: 'state'
    },
    elConfig: {
      formType: 'Select',
      placeholder: '任务状态',
      options: stateOptions
    }
  },
  'userId': {
    itemConfig: {
      name: 'userId'
    },
    elConfig: {
      formType: 'SearchSelect',
      placeholder: '操作人',
      fetchOptions: handleRefundUserList,
    }
  },
  'time': {
    itemConfig: {
      name: 'time'
    },
    elConfig: {
      formType: 'rangTime',
      placeholder: ['创建开始时间', '创建结束时间'],
      prefix: '创建时间',
    }
  },
}

//获取操作人列表
function handleRefundUserList(username) {
  return new Promise(async (resolve, reject) => {
    const { data, success } = await queryTakeUserList({ 
      username,
      current: 1,
      size: 20,
    });
    if (success) {
      const newArr = [];
      data.map(item => {
        newArr.push({
          label: item.name ? `${item.name}（${item.account}）` : item.account,
          value: item.id,
        })
      })
      resolve(newArr);
    } else {
      reject();
    }
  })
}

//处理表单数据
export const getFormItem = (optionsData) => {
  const arr = [];
  Object.keys(formItemData).map(v => {
    if(optionsData && optionsData[v]) {
      formItemData[v].elConfig.options = optionsData[v];
    }
    arr.push(formItemData[v]);
  })
  return arr;
}